﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace cjwygl
{
    public partial class FrmOpUpdate : Form
    {
        public int updateID = -1;
        public FrmOpUpdate()
        {
            InitializeComponent();
            updateID = -1;
            this.Text = "添加操作员";
        }
        public FrmOpUpdate(int id)
        {
            InitializeComponent();
            updateID = id;
            using (DataBase db = new DataBase())
            {
                string sql = "select USERNAME,NICKNAME from CJ_ADMIN where ID=@ID";
                db.AddParameter("ID", id);
                DataTable dt = db.ExecuteDataTable(sql);
                if (dt.Rows.Count == 0)
                {
                    MessageBox.Show("系统错误，无此条记录!");
                    return;
                }
                else
                {
                    textBox_username.Text = dt.Rows[0][0].ToString();
                    this.Text = "更新 "+dt.Rows[0][0].ToString()+"的记录";
                    textBox_nickname.Text = dt.Rows[0][1].ToString();
                    label_msg.Text = "若不更新密码则留空";
                }
            }
        }
        private void FrmOpUpdate_Load(object sender, EventArgs e)
        {
            //initialize combobox_role
            using (DataBase db = new DataBase())
            {
                List<ComboItem> roles = new List<ComboItem>();
                string sql = "select id,role_name from cj_role order by role_name desc";
                DataTable dt = db.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    roles.Add(new ComboItem 
                    {
                        Text=dr[1].ToString(),
                        Value=dr[0].ToString()
                    });
                }
                comboBox_role.DisplayMember = "Text";
                comboBox_role.ValueMember = "Value";
                comboBox_role.DataSource = roles;
            }
        }

        private void button_confirm_Click(object sender, EventArgs e)
        {
            string username = textBox_username.Text;
            string password = textBox_password.Text;
            string pwdconfirm = textBox_pwdconfirm.Text;
            string nickname = textBox_nickname.Text;
            nickname = nickname == "" ? username : nickname;
            string role_id = comboBox_role.SelectedValue.ToString();
            //check
            if (username == "")
            {
                MessageBox.Show("请填上用户名");
            }
            else if (password != pwdconfirm)
            {
                MessageBox.Show("两次密码输入不一样");
            }
            else
            {
                string sql = "";
                if (updateID == -1)
                {
                    if (password == "")
                    {
                        MessageBox.Show("未设置密码!!");
                        return;
                    }
                    //add record
                    using (DataBase db = new DataBase())
                    {
                        sql = "INSERT INTO CJ_ADMIN(USERNAME,PASSWORD,NICKNAME,ROLE_ID)"+
                            " VALUES(@USERNAME,@PASSWORD,@NICKNAME,@ROLE_ID)";
                        db.AddParameter("USERNAME", username);
                        db.AddParameter("PASSWORD", password);
                        db.AddParameter("ROLE_ID", role_id);
                        db.AddParameter("NICKNAME", nickname);
                        if (db.ExecuteNonQuery(sql) == 1)
                        {
                            MessageBox.Show("添加成功");
                            this.Close();
                        }

                    }
                }
                else
                {
                    using (DataBase db = new DataBase())
                    {
                        //edit a record
                        sql = "UPDATE CJ_ADMIN SET NICKNAME=@NICKNAME,ROLE_ID=@ROLE_ID";
                        db.AddParameter("NICKNAME", nickname);
                        db.AddParameter("ROLE_ID", role_id);
                        if (password != "")
                        {
                            sql += ",PASSWORD=@PASSWORD";
                            db.AddParameter("PASSWORD", password);
                        }
                        sql += " WHERE ID=@ID";
                        db.AddParameter("ID", updateID);
                        if (db.ExecuteNonQuery(sql) == 1)
                        {
                            MessageBox.Show("更改成功");
                            this.Close();
                        }
                    }
                    
                }
            }
            
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
